package com.dotmedia.dboperations;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.dotmedia.core.DatabaseAccessService;
import com.dotmedia.entities.SystemEntity;

/**
 * This class will have the database operation related logic for System entity.
 * 
 * @author siddharthsaha
 * 
 */
public class SystemDatabaseOperations {

	private static Logger logger = Logger.getLogger("SystemDatabaseOperations");

	/**
	 * This method will persist a system entity into the database
	 * 
	 * @param systemEntity
	 *        The SystemEntity which will be persisted into the database
	 */
	public void persistSystemEntity(SystemEntity systemEntity) {
		logger.info("IN method persistSystemEntity() in SystemDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		logger.info("Persisting an entity of SystemEntity to the database");
		int persistStatus = databaseAccessService.persistEntity(systemEntity);
		if (persistStatus == 0) {
			logger.info("The SystemEntity was successfully persisted into the database");
		} else {
			logger.error("The SystemEntity was not successfully persisted into the database");
		}
		logger.info("OUT method persistSystemEntityt() in SystemDatabaseOperations");
	}

	/**
	 * This method will remove a SystemEntity from the database
	 * 
	 * @param systemEntity
	 *        The SystemEntity which will be removed from the database
	 */
	public void removeSystemEntity(SystemEntity systemEntity) {
		logger.info("IN method removeSystemEntity() in SystemDatabaseOperations");
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		logger.info("Removing a SystemEntity from the database. SystemExtId: " + systemEntity.getSystemExtId());
		int removeStatus = databaseAccessService.removeEntity(systemEntity, systemEntity.getId());
		if (removeStatus == 0) {
			logger.info("The SystemEntity was successfully removed from the database. SystemExtId: " + systemEntity.getSystemExtId());
		} else {
			logger.info("The SystemEntity was not successfully removed from the database. SystemExtId: " + systemEntity.getSystemExtId());
		}
		logger.info("OUT method removeSystemEntity() in SystemDatabaseOperations");
	}

	/**
	 * This method will update the system entity.
	 * 
	 * @param systemEntity
	 *        The system entity that needs to be updated
	 */
	public void updateSystemEntity(SystemEntity systemEntity) {
		logger.info("IN method updateSystemEntity() in SystemDatabasOperations");
		String query = "SELECT systemEntity FROM SystemEntity systemEntity where systemEntity.id = " + systemEntity.getId();
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrivedEntities = databaseAccessService.getEntities(query);
		boolean obtained = false;
		for (int retrievedEntitesIndex = 0; retrievedEntitesIndex < retrivedEntities.size(); retrievedEntitesIndex++) {
			SystemEntity currentSystemEntity = (SystemEntity) retrivedEntities.get(retrievedEntitesIndex);
			if (currentSystemEntity.getId().intValue() == systemEntity.getId().intValue()) {
				logger.info("SystemEntity to be updated obtained");
				obtained = true;
				currentSystemEntity.setDefaultLangCode(systemEntity.getDefaultLangCode());
				currentSystemEntity.setDescription(systemEntity.getDescription());
				currentSystemEntity.setIsActive(systemEntity.getIsActive());
				currentSystemEntity.setExpireOn(systemEntity.getExpireOn());
				int updateStatus = databaseAccessService.updateEntity(currentSystemEntity);
				if (updateStatus == 0) {
					logger.info("SystemEntity was successfully updated");
				} else {
					logger.error("SystemEntity cannot be successfully updated");
				}
				break;
			}
		}
		if (!obtained) {
			logger.warn("SystemEntity was not updated because it was not found in the database");
		}
		logger.info("OUT method updateSystemEntity() in SystemDatabaseOperations");
	}

	/**
	 * This method returns the SystemEntity entities with a specific system ext id.
	 * 
	 * @param systemExtId
	 *        The system ext id to search
	 * @return
	 */
	public List<SystemEntity> getSystemEntitiesWithExtId(String systemExtId) {
		logger.info("IN method getSystemEntitiesWithExtId in SystemDatabaseOperations");
		List<SystemEntity> toReturnEntities = new ArrayList<SystemEntity>();
		String query = "SELECT systemEntity FROM SystemEntity systemEntity where systemEntity.systemExtId = '" + systemExtId + "'";
		logger.info("Query : " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int retreivedEntitiesIndex = 0; retreivedEntitiesIndex < retreivedEntities.size(); retreivedEntitiesIndex++) {
			toReturnEntities.add((SystemEntity) retreivedEntities.get(retreivedEntitiesIndex));
		}
		logger.info("OUT method getSystemEntitiesWithExtId in SystemDatabaseOperations");
		return toReturnEntities;
	}

	/**
	 * This method returns a list of system entities which are active
	 * 
	 * @return
	 */
	public List<SystemEntity> getActiveSystems() {
		logger.info("IN method getActiveSystems() in SystemDatabaseOperations");
		List<SystemEntity> toReturnEntities = new ArrayList<SystemEntity>();
		String query = "SELECT systemEntity FROM SystemEntity systemEntity where systemEntity.isActive = " + true;
		logger.info("Query : " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int retreivedEntitiesIndex = 0; retreivedEntitiesIndex < retreivedEntities.size(); retreivedEntitiesIndex++) {
			SystemEntity currentSystemEntity = (SystemEntity) retreivedEntities.get(retreivedEntitiesIndex);
			if (currentSystemEntity.getIsActive()) {
				// this is active
				toReturnEntities.add(currentSystemEntity);
			}
		}
		logger.info("OUT method getActiveSystems() in SystemDatabaseOperations");
		return toReturnEntities;
	}

	/**
	 * This method gets the system entities with the lang code passed to the method
	 * 
	 * @param langCode
	 *        The lang code whose system entity is required
	 * @return
	 */
	public List<SystemEntity> getSystemEntitiesWithLangCode(String langCode) {
		logger.info("IN method getSystemEntitiesWithLangCode() in SystemDatabaseOperations");
		List<SystemEntity> toReturnEntities = new ArrayList<SystemEntity>();
		String query = "SELECT systemEntity FROM SystemEntity systemEntity WHERE systemEntity.defaultLangCode.langCode = '" + langCode + "'";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retrievedEntities = databaseAccessService.getEntities(query);
		for (int retreivedEntitiesIndex = 0; retreivedEntitiesIndex < retrievedEntities.size(); retreivedEntitiesIndex++) {
			SystemEntity currentSystemEntity = (SystemEntity) retrievedEntities.get(retreivedEntitiesIndex);
			if (currentSystemEntity.getDefaultLangCode().getLangCode().equals(langCode)) {
				// this is to be added
				toReturnEntities.add(currentSystemEntity);
			}
		}
		logger.info("OUT method getSystemEntitiesWithLangCode() in SystemDatabaseOperations");
		return toReturnEntities;
	}

	/**
	 * This method returns all the system entities
	 * 
	 * @return A list of all the system entities
	 */
	public List<SystemEntity> getAllSystemEntities() {
		logger.info("IN method getAllSystemEntities() in SystemDatabaseOperations");
		List<SystemEntity> toReturnList = new ArrayList<SystemEntity>();
		String query = "SELECT systemEntity FROM SystemEntity systemEntity";
		logger.info("Query: " + query);
		DatabaseAccessService databaseAccessService = DatabaseAccessService.getInstance();
		List retreivedEntities = databaseAccessService.getEntities(query);
		for (int index = 0; index < retreivedEntities.size(); index++) {
			SystemEntity currentSystemEntity = (SystemEntity) retreivedEntities.get(index);
			toReturnList.add(currentSystemEntity);
		}
		logger.info("OUT method getAllSystemEntities() in SystemDatabaseOperations");
		return toReturnList;
	}
}
